import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },

    //购物车
    {
      path: '/cart',
      name: 'cart',
      component: () => import('../views/CartView.vue'),
    },
    //分类
    {
      path: '/category',
      name: 'category',
      component: () => import('../views/CategoryView.vue'),
    },
    //我的
    {
      path: '/my',
      name: 'my',
      component: () => import('../views/MyView.vue'),
    },
    //注册
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
    },
    //登录
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    },
    //用户信息
    {
      path: '/userinfo',
      name: 'userinfo',
      component: () => import('../views/UserInfoView.vue'),
    },

    //商品详情
    {
      path: '/detail',
      name: 'detail',
      component: () => import('../views/ProDetailView.vue'),
    },
    //搜索结果
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/SearchResultView.vue'),
    },
    //修改昵称
    {
      path: '/updatenickname',
      name: 'updatenickname',
      component: () => import('../views/NickNameUpdateView.vue'),
    },
    //修改密码
    {
      path: '/updatepassword',
      name: 'updatepassword',
      component: () => import('../views/PasswordUpdateView.vue'),
    },
    //修改性别
    {
      path: '/updategender',
      name: 'updategender',
      component: () => import('../views/GenderUpdateView.vue'),
    },
    //修改年龄
    {
      path: '/updateage',
      name: 'updateage',
      component: () => import('../views/AgeUpdateView.vue'),
    },
    //修改生日
    {
      path: '/updatebirthday',
      name: 'updatebirthday',
      component: () => import('../views/BirthdayUpdateView.vue'),
    },
    //修改收货地址
    {
      path: '/addressedit',
      name: 'addressedit',
      component: () => import('../views/AddressEditView.vue'),
    },
    //我的订单
    {
      path: '/myorder',
      name: 'myorder',
      component: () => import('../views/MyOrderView.vue'),
    },
    //我的订单
    {
      path: '/orderdetail',
      name: 'orderdetail',
      component: () => import('../views/OrderDetailView.vue'),
    },
  ],
})
//路由守卫
router.beforeEach((to) => {
  let blackList = ['/cart', '/my', '/updatepassword', '/updatenickname']
  if (blackList.includes(to.path)) {
    let token = localStorage.getItem('mall-token')
    if (token == null) {
      // eslint-disable-next-line no-undef
      showFailToast('请先登录')
      return { path: '/login' }
    } else {
      return true
    }
  } else {
    return true
  }
})
export default router
